package com.syni.mdd.sjb.common.datatreaure.service.impl.databank.saasminapp;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshMinappMemberGrade;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshMinappMemberGrade;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.HomeMemberDataRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.MemberAnalysisRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.MemberGradeRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.MemberListRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.HomeMemberDataRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.MemberRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.*;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.UpdateRuleRqVo;
import com.syni.mdd.sjb.common.datatreaure.service.*;
import com.syni.mdd.sjb.common.datatreaure.service.DxshMinappMemberGradeService;
import com.syni.mdd.sjb.common.datatreaure.service.databank.saasminapp.MemberService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class MemberServiceImpl implements MemberService {
    @Autowired
    private UserRestService userRestService;
    @Autowired
    private DxshMinappMemberGradeService dxshMinappMemberGradeService;

    @Override
    public Map<String, Object> getIndexData(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/statistics/getIndexData";
            Map<String,Object> params = new HashMap<>();
            params.put("storeId", memberRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, memberRqVo.getToken(), memberRqVo.getUserId(),true,params, params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                Map<String,Object> memberMap = JSON.parseObject(JSON.toJSONString(resultMap.get("data")),Map.class);
                List<MemberGradeRpVo> memberLevelList = JSON.parseArray(JSON.toJSONString(memberMap.get("memberLevelList")), MemberGradeRpVo.class);
                for(MemberGradeRpVo m : memberLevelList){
                    if(m.getName() != null){
                        DxshMinappMemberGrade dxshMinappMemberGrade = dxshMinappMemberGradeService.findByMemberName(m.getName());
                        if(dxshMinappMemberGrade == null){
                            dxshMinappMemberGrade = new DxshMinappMemberGrade();
                            dxshMinappMemberGrade.setMemberGrade(m.getGrade());
                            dxshMinappMemberGrade.setMemberName(m.getName());
                            dxshMinappMemberGrade.setNewTime(DateTimeUtils.converDateToLocalDateTime(new Date()));
                            dxshMinappMemberGradeService.save(dxshMinappMemberGrade);
                        }
                    }
                }
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getAnalysisData(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/statistics/getAnalysisData";
            Map<String,Object> params = new HashMap<>();
            params.put("storeId", memberRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, memberRqVo.getToken(), memberRqVo.getUserId(),true,params, params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                Map<String,Object> memberMap = JSON.parseObject(JSON.toJSONString(resultMap.get("data")),Map.class);
                List<MemberAnalysisRpVo> analysisData = JSON.parseArray(JSON.toJSONString(memberMap.get("analysisData")), MemberAnalysisRpVo.class);
                if(analysisData.size() > 0){
                    for(MemberAnalysisRpVo m : analysisData){
                        DxshMinappMemberGrade dxshMinappMemberGrade = dxshMinappMemberGradeService.findByMemberName(m.getMemberLevelName());
                        if(dxshMinappMemberGrade != null){
                            m.setMemberGrade(dxshMinappMemberGrade.getMemberGrade());
                        }
                    }
                    memberMap.put("analysisData",analysisData);
                }

                List<MemberListRpVo> memberList = JSON.parseArray(JSON.toJSONString(memberMap.get("memberList")), MemberListRpVo.class);
                if(memberList.size() > 0){
                    for(MemberListRpVo m : memberList){
                        if(StringUtils.equals(m.getName(),"全部会员")){
                            m.setMemberGrade(0);
                        }else{
                            DxshMinappMemberGrade dxshMinappMemberGrade = dxshMinappMemberGradeService.findByMemberName(m.getName());
                            if(dxshMinappMemberGrade != null){
                                m.setMemberGrade(dxshMinappMemberGrade.getMemberGrade());
                            }
                        }
                    }
                    memberMap.put("memberList",memberList);
                }

                resultMap.put("data", memberMap);
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getNetShopData(MemberRqVo memberRqVo) {
        try{
            String sendUrl = "oms/OmsOrder/getNetShopData?storeId=" + memberRqVo.getStoreId();
            String sendUrlHome = "oms/OmsOrder/getRoomFoodData?storeId=" + memberRqVo.getStoreId();
            String postUrl = AppConfig.getProperty("saas.dxshapp.order.service");
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(memberRqVo.getUserId(),memberRqVo.getToken());
            HomeMemberDataRpVo homeMemberDataRpVo = new HomeMemberDataRpVo();
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(postUrl + sendUrl,new HashMap<>(),RestTemplateUtils.getSassHeader(minitoken));
                String reponseBodyHome = RestTemplateUtils.postForEntityForJsonByLoadBalanced(postUrl +sendUrlHome,new HashMap<>(),RestTemplateUtils.getSassHeader(minitoken));
                Map<String,Object> resultMap = JSON.parseObject(reponseBody,Map.class);
                Map<String,Object> resultMapHome = JSON.parseObject(reponseBodyHome,Map.class);
                if(ResultMapHelper.isSuccess(resultMap)){
                    BeanUtils.copyProperties(JSON.parseObject(JSON.toJSONString(resultMap.get("data")), HomeMemberDataRpVo.class),homeMemberDataRpVo);
                }
                if(ResultMapHelper.isSuccess(resultMapHome)){
                    Map<String,Object> mapData = JSON.parseObject(JSON.toJSONString(resultMapHome.get("data")), Map.class);
                    homeMemberDataRpVo.setOrderCountHome(Integer.parseInt(String.valueOf(mapData.get("orderCount"))));
                    homeMemberDataRpVo.setAmountHome(Double.parseDouble(String.valueOf(mapData.get("amount"))));
                }
            }
            return ResultMapHelper.success(homeMemberDataRpVo,ResultMsg.HAND_SUCCESS);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getServiceInfo(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/service/getServiceInfo";
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(memberRqVo.getUserId(),memberRqVo.getToken());
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> sassHeader = RestTemplateUtils.getSassHeader(minitoken);
                sassHeader.put("storeId", memberRqVo.getStoreId());
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(postUrl,new HashMap<>(),sassHeader);
                Map<String,Object> resultMap = JSON.parseObject(reponseBody,Map.class);
                if(ResultMapHelper.isSuccess(resultMap)){
                    return ResultMapHelper.success(resultMap.get("data"),ResultMsg.HAND_SUCCESS);
                }else{
                    return ResultMapHelper.failure(resultMap.get("msg").toString());
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return  ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }



    @Override
    public Map<String, Object> getGrowRules(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/growth/rule/get";
            Map<String,Object> params = new HashMap<>();
            params.put("storeId", memberRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, memberRqVo.getToken(), memberRqVo.getUserId(),true,params, params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> updateGrowRules(UpdateRuleRqVo updateRuleRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/growth/rule/update";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId",updateRuleRqVo.getStoreId());
            Map<String, Object> resultMap = postReqMember(postUrl, updateRuleRqVo.getToken(), updateRuleRqVo.getUserId(),true,headMap, BeanUtils.objectToMap(updateRuleRqVo));
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return  ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getGrowRulesTimes(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/setting/modify/limit/get";
            Map<String,Object> params = new HashMap<>();
            params.put("storeId", memberRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, memberRqVo.getToken(), memberRqVo.getUserId(),true,params, params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> updateMemberLevel(UpdateLevelRqVo updateLevelRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/level/update";
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(updateLevelRqVo.getUserId(),updateLevelRqVo.getToken());
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> params = BeanUtils.objectToMap(updateLevelRqVo);
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(postUrl,params,RestTemplateUtils.getSassHeader(minitoken));
                Map<String,Object> resultMap = JSON.parseObject(reponseBody,Map.class);
                if(StringUtils.equals(resultMap.get("code").toString(),"200")){
                    return ResultMapHelper.success("保存成功",ResultMsg.HAND_SUCCESS);
                }else{
                    return ResultMapHelper.failure(resultMap.get("msg").toString());
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return  ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getLevelConfig(GetLevelConfigRqVo getLevelConfigRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.member.service") + "ums/member/level/get";
            Map<String,Object> params = new HashMap<>();
            params.put("id", getLevelConfigRqVo.getId());
            Map<String, Object> resultMap = reqMember(postUrl, getLevelConfigRqVo.getToken(), getLevelConfigRqVo.getUserId(), params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> countDataByStore(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/rechargeRule/countDataByStore";
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("storeId", memberRqVo.getStoreId());
            Map<String, Object> resultMap = postReqMember(postUrl, memberRqVo.getToken(), memberRqVo.getUserId(),true, extraMap ,new HashMap<>());
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> recordListByStore(RecordByStoreVo recordByStoreVo) {
        try{
            String postUrl = "";
            if(recordByStoreVo.getType() != null){
                postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/rechargeRule/recordListByStore?type=" + recordByStoreVo.getType();
            }else{
                postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/rechargeRule/recordListByStore" ;
            }

            //请求头
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("storeId", recordByStoreVo.getStoreId());
            extraMap.put("appid", recordByStoreVo.getAppid());
            //请求体
            Map<String,Object> params = new HashMap<>();
            Map<String, Object> resultMap = postReqMember(postUrl, recordByStoreVo.getToken(), recordByStoreVo.getUserId(),true, extraMap ,params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getRechargeRule(RechargeRuleVo rechargeRuleVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/rechargeRule/getRule";
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("appid", rechargeRuleVo.getAppid());
            Map<String, Object> resultMap = postReqMember(postUrl, rechargeRuleVo.getToken(), rechargeRuleVo.getUserId(),true, extraMap ,new HashMap<>());
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> setRechargeRule(RechargeRuleVo rechargeRuleVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/rechargeRule/setRule";
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("appid", rechargeRuleVo.getAppid());

            Map<String, Object> params = BeanUtils.objectToMap(rechargeRuleVo);
            Map<String, Object> resultMap = postReqMember(postUrl, rechargeRuleVo.getToken(), rechargeRuleVo.getUserId(),true, extraMap ,params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getSeckillList(SeckillListVo seckillListVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsPromo/getPage?storeId=" + seckillListVo.getStoreId() +
                    "&status=" + seckillListVo.getStatus() + "&pageNo=" + seckillListVo.getPageNum() + "&pageSize=" + seckillListVo.getPageSize();
            Map<String, Object> resultMap = postFormReqMember(postUrl, seckillListVo.getToken(), seckillListVo.getUserId(),false, new HashMap<>() ,new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getSeckillDetail(SeckillDetailVo seckillDetailVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsPromo/" + seckillDetailVo.getId();
            Map<String, Object> resultMap = reqMember(postUrl, seckillDetailVo.getToken(), seckillDetailVo.getUserId(),new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.result(ResultMsg.GOODS_NOT_EXIST,resultMap.get("msg").toString());
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> createSeckill(CreateSeckillVo createSeckillVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsPromo/create";
            Map<String, Object> params = BeanUtils.objectToMap(createSeckillVo);
            Map<String, Object> resultMap = postReqMember(postUrl, createSeckillVo.getToken(), createSeckillVo.getUserId(),false, new HashMap<>() ,params);
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> updateSeckill(UpdateSeckillVo updateSeckillVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsPromo/update";
            Map<String, Object> params = BeanUtils.objectToMap(updateSeckillVo);
            Map<String, Object> resultMap = postReqMember(postUrl, updateSeckillVo.getToken(), updateSeckillVo.getUserId(),false, new HashMap<>() ,params);
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> endSeckill(SeckillDetailVo seckillDetailVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsPromo/end/" + seckillDetailVo.getId();
            Map<String, Object> resultMap = postReqMember(postUrl, seckillDetailVo.getToken(), seckillDetailVo.getUserId(),false, new HashMap<>() ,new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getGoodList(GoodListVo goodListVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.goods.service") + "notAuth/categoryAndGoodsList/list";
            Map<String,Object> params = new HashMap<>();
            params.put("storeId", goodListVo.getStoreId());
            params.put("keywords", goodListVo.getKeywords());
            params.put("manageType", goodListVo.getManageType());
            Map<String, Object> resultMap = reqMember(postUrl, goodListVo.getToken(), goodListVo.getUserId(), params);
            if(resultMap != null && StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getGroupBookingList(SeckillListVo seckillListVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/getPage?storeId=" + seckillListVo.getStoreId() +
                    "&status=" + seckillListVo.getStatus() + "&pageNo=" + seckillListVo.getPageNum() + "&pageSize=" + seckillListVo.getPageSize();
            Map<String, Object> resultMap = postFormReqMember(postUrl, seckillListVo.getToken(), seckillListVo.getUserId(),false, new HashMap<>() ,new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getGroupBookingDetail(SeckillDetailVo seckillDetailVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/" + seckillDetailVo.getId();
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId",seckillDetailVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, seckillDetailVo.getToken(), seckillDetailVo.getUserId(),true,headMap,new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }


    @Override
    public Map<String, Object> createGroupBookingDetail(CreateGroupBookingVo createGroupBookingVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/create";
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("storeId",createGroupBookingVo.getStoreId());
            Map<String, Object> params = BeanUtils.objectToMap(createGroupBookingVo);
            Map<String, Object> resultMap = postReqMember(postUrl, createGroupBookingVo.getToken(), createGroupBookingVo.getUserId(),true, extraMap ,params);
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }



    @Override
    public Map<String, Object> updateGroupBookingDetail(UpdateGroupBookingVo updateGroupBookingVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/update";
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("storeId",updateGroupBookingVo.getStoreId());
            Map<String, Object> params = BeanUtils.objectToMap(updateGroupBookingVo);
            Map<String, Object> resultMap = postReqMember(postUrl, updateGroupBookingVo.getToken(), updateGroupBookingVo.getUserId(),true, extraMap ,params);
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> endGroupBookingDetail(SeckillDetailVo seckillDetailVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/end/" + seckillDetailVo.getId();
            Map<String,Object> extraMap = new HashMap<>();
            extraMap.put("storeId",seckillDetailVo.getStoreId());
            Map<String, Object> resultMap = postReqMember(postUrl, seckillDetailVo.getToken(), seckillDetailVo.getUserId(),true, extraMap ,new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getUserGroupBooking(SeckillDetailVo seckillDetailVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsGroup/getGroupList/" + seckillDetailVo.getId();
            Map<String, Object> resultMap = reqMember(postUrl, seckillDetailVo.getToken(), seckillDetailVo.getUserId(),new HashMap<>());
            if(ResultMapHelper.isSuccess(resultMap)){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(String.valueOf(resultMap.get("msg")));
            }
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }


    public Map<String,Object> getReqWithHead(String requestUrl,String token,Integer userId,Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> reqMember(String requestUrl,String token,Integer userId,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,RestTemplateUtils.getSassHeader(minitoken));
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMemberWithToken(String requestUrl,String minToken,Integer userId,
                                            Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minToken);
            if(isExtra){
                saasHeader.putAll(extraMap);
            }
            String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
            resultMap = JSON.parseObject(reponseBody,Map.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postFormReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntity(RestTemplateUtils.getLoadRestTemplate(),requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }
}
